package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.microsoft.schemas.office.visio.x2012.main.MasterContentsDocument;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 分页查询
     * @param setmealPageQueryDTO 套餐分页查询传输模型
     * @return PageResult
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * 新增套餐
     * @param setmealDTO 新增-修改菜品传递的数据模型
     */
    @Transactional
    public void InsertSetmealWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        setmealDishMapper.insertSetmealDishS(setmealDishes);
    }

    /**
     * 套餐起售停售
     * @param status 套餐状态，1表示起售，0表示停售
     * @param id 套餐id
     */
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE){
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            if (dishes != null && dishes.size() > 0){
                dishes.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE){
                        throw new com.sky.exception.DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐
     * @param id 套餐id
     * @return SetmealVO
     */
    public SetmealVO getSetmealWithDish(Long id) {
        // 1、查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        // 2、查询菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getdish(id);
        // 3、将套餐数据和菜品数据封装在套餐响应模型中
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO 新增-修改菜品传递的数据模型
     */
    @Transactional
    public void updateSetmealWithDish(SetmealDTO setmealDTO) {
        // 1、封装setmeal实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        Long setmealId = setmeal.getId();
        // 2、获取套餐图片url地址
        Setmeal oldSetmeal = setmealMapper.getById(setmeal.getId());
        // 3、删除阿里云图片
        if (!oldSetmeal.getImage().equals(setmeal.getImage())){
            aliOssUtil.deleteImage(oldSetmeal.getImage());
        }
        // 4、修改setmeal实体类
        setmealMapper.update(setmeal);
        // 5、删除套餐菜品关系数据
        setmealDishMapper.deleteByDishId(setmealId);
        // 6、重新插入套餐菜品关系数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertSetmealDishS(setmealDishes);
    }

    /**
     * 批量删除套餐
     * @param ids 套餐集合
     */
    @Transactional
    public void delete(List<Long> ids) {
        // 1、判断套餐是否起售
        List<Setmeal> setmeals = setmealMapper.getByIds(ids);
        setmeals.forEach(setmeal ->{
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        // 2、删除套餐数据
        setmealMapper.delete(ids);
        // 3、删除套餐菜品数据
        setmealDishMapper.deleteByDishIds(ids);
        // 4、删除阿里云图片数据
        setmeals.forEach(setmeal -> {
            aliOssUtil.deleteImage(setmeal.getImage());
        });
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId 分类id
     * @return Setmeal套餐集合
     */
    public List<Setmeal> list(Long categoryId) {
        // 1、创建setmeal套餐实体类对象，封装查询条件
        Setmeal setmeal = new Setmeal();
        // 1.1、设置套餐分类id
        setmeal.setCategoryId(categoryId);
        // 1.2、设置套餐起售状态
        setmeal.setStatus(StatusConstant.ENABLE);
        // 2、查询套餐数据集合
        List<Setmeal> setmealList = setmealMapper.list(setmeal);
        return setmealList;
    }

    /**
     * 根据套餐id查询菜品选项
     * @param id 套餐id
     * @return DishItemVO集合
     */
    public List<DishItemVO> getDishItemById(Long setmealId) {
        List<DishItemVO> DishItemVOList = setmealMapper.getDishItemBySetmealId(setmealId);
        return DishItemVOList;
    }
}
